package com.shengdong.water.feature.main.fragment

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.shengdong.water.R
import com.shengdong.water.base.BaseVMFragment
import com.shengdong.water.code.adapter.NotifyMsgAdapter
import com.shengdong.water.code.config.RouterConfig
import com.shengdong.water.code.utils.StatusBarUtil
import com.shengdong.water.code.viewmodel.WorkViewModel
import com.shengdong.water.code.config.UserConfig
import com.shengdong.water.code.router.RouterManager
import com.shengdong.water.databinding.FragmentWorkbenchBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class WorkbenchFragment : BaseVMFragment<FragmentWorkbenchBinding, WorkViewModel>() {


    override fun getLayoutId() = R.layout.fragment_workbench

    override fun createViewModel(): WorkViewModel = viewModelProvider(WorkViewModel::class.java)

    override fun useImmersiveStatusBar(): Boolean = true
    override fun showStatusBar() = false

    private val notifyMsgAdapter = NotifyMsgAdapter()

    override fun initStatusBar() {
        // 设置沉浸式状态栏
        StatusBarUtil.setImmersiveStatusBar(requireActivity())
        // 设置状态栏颜色
        StatusBarUtil.setStatusBarColor(requireActivity(), statusBarColor())
        // 设置状态栏明暗模式
        StatusBarUtil.setStatusBarDarkMode(requireActivity(), statusBarDarkMode())
        // 设置状态栏可见性
        StatusBarUtil.setStatusBarVisibility(requireActivity(), showStatusBar())
    }

    override fun initViews() {
        binding.viewModel = viewModel
//        binding.listAlarm.layoutManager = LinearLayoutManager(requireContext())
        binding.listAlarm.setHasFixedSize(true)
        setupListener()
        UserConfig.user?.let {
            binding.tvPlace.text = it.address

        }
        binding.listAlarm.adapter = notifyMsgAdapter



    }

    private  fun setupListener() {
        binding.ivSign.setOnClickListener {
            RouterManager.navigate(RouterConfig.SIGN_PATH)
        }
        binding.ivManager.setOnClickListener {
            RouterManager.navigate(RouterConfig.MANAGE_PATH)
        }
        binding.ivScan.setOnClickListener {
            RouterManager.navigate(RouterConfig.QRCODE_SCAN_PATH)
        }

        binding.tvMore.setOnClickListener {
            RouterManager.navigate(RouterConfig.NOTICE_PATH )
        }

        binding.btnSearch.setOnClickListener {
            RouterManager.build(RouterConfig.SEARCH_PATH)
                .withString("name", binding.editSearch.text.toString())
                .navigate()
        }

    }

    override fun initData() {
        viewModel.getNotify()
    }

    override fun dataObserver() {
        // 观察 ViewModel 中的数据变化
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewModel.notifyMsg.collect { notifyMsg ->
                    notifyMsg?.let {
                        if(it.records.isEmpty()){
                            binding.listAlarm.setEmptyView(binding.layoutEmpty.root)
                            return@collect
                        }
                        notifyMsgAdapter.setItems(it.records)
                    }
                }
            }
        }
    }


}